package com.tool.tcpimg;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.*;

import org.apache.commons.lang.StringUtils;
import org.come.bean.AllSuit;
import org.come.bean.RoleSuitBean;
import org.come.until.CutButtonImage;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;

import com.tool.tcp.Animation;
import com.tool.tcp.Frame;
import com.tool.tcp.SpriteFactory;


//TODO add Link

/**
 * 文字格式：聊天时可以为消息内容设置文字格式，指令为“#字母”，区分大小写。 <br>
 * #R 表示后面的字体为红色(red)<br>
 * #G 表示后面的字体为绿色(green)<br>
 * #B 表示后面的字体为蓝色(blue)<br>
 * #K 表示后面的字体为黑色(black)<br>
 * #Y 表示后面的字体为黄色(yellow)<br>
 * #W 表示后面的字体为白色(white)<br>
 * #b 表示后面的字体为闪烁(blink)<br>
 * #c + 六个数字或者A-F字母 自定义颜色，例如：c008000=暗绿色<br>
 * #u + 文字 + #u 文字有下划线。<br>
 * #n 所有文字状态恢复正常。<br>
 * #r 文字换行。<br>
 * ## 输出一个#号。<br>
 * #0-99 动画 //系统400 信息401 世界402 当前403 队伍404 帮派405 #T系统 #X 信息 #J 世界 #Q 当前 #D 队伍 #P 帮派
 * #V 点击监听
 *
 * @author 龚德伟
 * @history 2008-6-9 龚德伟 新建
 */
public class RichLabel extends JComponent {

    private static final long serialVersionUID = 4898130145332371300L;
    public ArrayList<Object> sectionList;
    //可交互的
    private boolean isInteractive;
    private long startTime;
    private long currTime;
    private int rowHeight = 0;
    private Font font;
    private String text;
    private static HashMap<String, Animation> faceAnimations;
    private static Pattern pattern;
    public static List<String> num = new ArrayList<>();

    static {
        faceAnimations = new HashMap<String, Animation>(100);
        // 聊天正则表达式(颜色-表情-字节定义)
        String REGULAR = "#([RGBKYWTXJESQDPVLnbur#]|[1][0-5][0-9]|[1][6][0-8]|[6][0][0-4][0-9]|[7][4-6][0-9]|[9][1-9][0-9][0-9][0-9][0-9]|[9][1-9][0-9][0-9][0-9]|[9][1-9][0-9][0-9]|[8][8][0-5]|[1-9]\\d|[0-9]|"
                + "c[0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)|"
                + "[^#]+";
        pattern = Pattern.compile(REGULAR);
    }

    private static String PATH = "resource/emoticons/";

    public RichLabel() {
        this(null, null);
    }

    public RichLabel(final String text) {
        this.point = new Point(0, 0);
        this.sectionList = new ArrayList();
        this.setBackground(Color.RED);
        this.setForeground(Color.BLUE);
        this.setIgnoreRepaint(true);
        this.setBorder(null);
        this.setOpaque(false);
        this.text = text;
        this.setSize(98, 16);
        this.parseText(text);
    }

    public RichLabel(String text, Font font) {
        sectionList = new ArrayList<Object>();
        setBackground(Color.RED);
        setForeground(Color.BLUE);
        setIgnoreRepaint(true);
        setBorder(null);
        setOpaque(false);
        this.text = text;
        setSize(14 * 7, 16);
        parseText(text);
        this.font = font;
    }

    public RichLabel(String text, Font font, int size) {
        sectionList = new ArrayList<Object>();
        setBackground(Color.RED);
        setForeground(Color.BLUE);
        setIgnoreRepaint(true);
        setBorder(null);
        setOpaque(false);
        this.text = text;
        parseText(text);
        this.font = font;
        setSize(computeSize(size));
    }

    public RichLabel(String text, Integer rowHeight, Font font) {
        sectionList = new ArrayList<Object>();
        setBackground(Color.RED);
        setForeground(Color.BLUE);
        setIgnoreRepaint(true);
        setBorder(null);
        setOpaque(false);
        this.text = text;
        setSize(14 * 7, 16);
        parseText(text);
        this.font = font;
        this.rowHeight = rowHeight;
    }

    //
    public void parseText(String text) {
        if (text == null)
            return;
        String section;
        Matcher m = pattern.matcher(text);
        Animation anim;
        // * //系统400 信息401 世界402 当前403 队伍404 帮派405
        // * #T 系统* #X 信息* #J 世界* #Q 当前* #D 队伍* #P 帮派
        while (m.find()) {
            section = m.group();
            if (section.startsWith("#")) {
                anim = faceAnimations.get(section);
                if (section.charAt(1) >= '0' && section.charAt(1) <= '9') {
                    String value = section.substring(1);
                    if (value.equals("880")) {
                        anim = new Animation();
                        Frame frame = new Frame(CutButtonImage.getImage("inkImg/old/1/gemBackImg.png", -1, -1).getImage(), 0, 0);
                        anim.addFrame(frame);
                    } else if (value.equals("881")) {
                        anim = new Animation();
                        Frame frame = new Frame(CutButtonImage.getImage("inkImg/old/1/gemImg3.png", -1, -1).getImage(), 0, 0);
                        anim.addFrame(frame);
                    } else if (value.equals("882")) {
                        anim = new Animation();
                        Frame frame = new Frame(CutButtonImage.getImage("inkImg/old/1/gemImg1.png", -1, -1).getImage(), 0, 0);
                        anim.addFrame(frame);
                    } else if (value.equals("883")) {
                        anim = new Animation();
                        Frame frame = new Frame(CutButtonImage.getImage("inkImg/old/1/gemImg2.png", -1, -1).getImage(), 0, 0);
                        anim.addFrame(frame);
                    } else if (value.equals("884")) {
                        anim = new Animation();
                        Frame frame = new Frame(CutButtonImage.getImage("inkImg/old/1/gemImg4.png", -1, -1).getImage(), 0, 0);
                        anim.addFrame(frame);
                    } else if (value.equals("885")) {
                        anim = new Animation();
                        Frame frame = new Frame(CutButtonImage.getImage("inkImg/old/1/gemImg5.png", -1, -1).getImage(), 0, 0);
                        anim.addFrame(frame);
                    } else
                        //套装图片
                        if (gettzm(text) != "" && Integer.parseInt(value) > 90 && !getjntp(Integer.parseInt(value)) && !getbstp(Integer.parseInt(value))) {
                            String v = gettzm(text);
                            String[] vs = v.split("=");
                            String[] vss = vs[1].split("\\|");
                            String sa = vs[0];
                            String sad = value.replace("9" + sa, "");
                            boolean bs = false;
                            if (num.size() > 0) {
                                for (String nu : num) {
                                    if (nu.equals(sad)) {
                                        num.remove(nu);
                                        bs = true;
                                        break;
                                    }
                                }
                            }
                            if (bs) {
                                anim = new Animation();
                                Image sx = CutButtonImage.getImage("img/item/tz" + sa + "_" + sad + ".png", 30, 30).getImage();
                                Frame frame = new Frame(sx, 0, 0);
                                anim.addFrame(frame);
                            } else {
                                anim = new Animation();
                                Frame frames = new Frame(CutButtonImage.getImage("inkImg/old/skill/fighting-skill/tz/htz" + sa + "_" + sad + ".png", 30, 30).getImage(), 0, 0);
                                anim.addFrame(frames);
                            }
                        } else
                            //套装技能图片
                            if (getjntp(Integer.parseInt(value))) {
                                anim = new Animation();
                                Frame frame = new Frame(CutButtonImage.getImage("inkImg/old/skill/fighting-skill/tzjn/" + value + ".png", 13, 13).getImage(), 0, 0);
                                anim.addFrame(frame);
                            } else
                                //宝石图片
                                if (getbstp(Integer.parseInt(value))) {
                                    anim = new Animation();
                                    Frame frame = new Frame(CutButtonImage.getImage("img/item/" + value + ".png", 17, 17).getImage(), 0, 0);
                                    anim.addFrame(frame);
                                } else {
                                    anim = SpriteFactory.loadAnimation(PATH + value + ".was");
                                }
                    if (anim != null)
                        faceAnimations.put(section, anim);
                } else if (section.equals("#T") || section.equals("#X") || section.equals("#J") || section.equals("#Q") || section.equals("#S") || section.equals("#D") || section.equals("#P") || section.equals("#E")) {
                    anim = faceAnimations.get(section);
                    if (anim == null) {
                        anim = SpriteFactory.loadAnimation(PATH + gethead(section.substring(1)) + ".was");
                        if (anim != null)
                            faceAnimations.put(section, anim);
                    }
                }

                if (anim != null) {
                    sectionList.add(anim);
                } else if (section.startsWith("#c")) {
                    if (section.length() == 8) {
                        Color color = UIUtils.getColor(section);
                        sectionList.add(color);
                    }
                } else if (section.equals("#R")) {
                    sectionList.add(Color.RED);
                } else if (section.equals("#G")) {
                    sectionList.add(Color.GREEN);
                } else if (section.equals("#B")) {
                    sectionList.add(Color.BLUE);
                } else if (section.equals("#W")) {
                    sectionList.add(Color.WHITE);
                } else if (section.equals("#K")) {
                    sectionList.add(Color.BLACK);
                } else if (section.equals("#Y")) {
                    sectionList.add(Color.YELLOW);
                } else if (section.equals("#r")) {
                    // line swap
                    sectionList.add(new Integer(-1));
                } else if (section.equals("#n")) {
                    // FIXME reset
                    sectionList.add(Color.WHITE);
                } else if (section.equals("#V")) {
                    try {
                        if (m.find()) {
                            String cr = m.group();
                            if (cr.startsWith("c4f0f0f")) {
                                sectionList.add(new InputBean(-1));
                                continue;
                            }
                            InputBean bean = GsonUtil.getGsonUtil().getgson().fromJson(cr, InputBean.class);
                            if (bean.getColor() != null && !bean.getColor().equals("")) {
                                Color color = null;
                                if (bean.getColor().startsWith("c")) {
                                    String color2 = section.substring(2);
                                    if (!color2.equals("")) {
                                        color = Color.decode("0x" + color2);
                                    }
                                } else if (bean.getColor().equals("R")) {
                                    color = Color.RED;
                                } else if (bean.getColor().equals("G")) {
                                    color = Color.GREEN;
                                } else if (bean.getColor().equals("B")) {
                                    color = Color.BLUE;
                                } else if (bean.getColor().equals("W")) {
                                    color = Color.WHITE;
                                } else if (bean.getColor().equals("K")) {
                                    color = Color.BLACK;
                                } else if (bean.getColor().equals("Y")) {
                                    color = Color.YELLOW;
                                }
                                bean.setColor2(color);
                            }
                            bean.setColor(null);
                            sectionList.add(bean);
                            if(StringUtils.isNotBlank(bean.getGoodNum())){
                                bean.setGoodNum(null);
                                InputBean bean2=GsonUtil.getGsonUtil().getgson().fromJson(cr, InputBean.class);
                                sectionList.add(bean2);
                                sectionList.add(Color.GREEN);
                                if(bean.getType() == 5){
                                    sectionList.add("]");
                                }else if (bean.getType() == 8) {
                                    sectionList.add("");
                                }else{
                                    sectionList.add("]");
                                }
                                sectionList.add(Color.WHITE);
                            }

                            isInteractive=true;
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            } else {
                sectionList.add(section);
            }
        }
    }

    public void addList(Object object) {
        sectionList.add(object);
    }

    public int gethead(String head) {
        if (head.equals("T")) return 400;
        else if (head.equals("X")) return 401;
        else if (head.equals("J")) return 402;
        else if (head.equals("Q")) return 403;
        else if (head.equals("D")) return 404;
        else if (head.equals("S")) return 405;
        else if (head.equals("E")) return 400;
        else return 406;
    }

    Graphics2D graphics2D;

    @Override
    public void paint(Graphics g2) {
        if (font != null) {
            g2.setFont(font);
        } else {
//			g2.setFont(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);

            g2.setFont(UIUtils.TEXT_FONT);
        }

        graphics2D = (Graphics2D) g2.create();
        graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        graphics2D.setColor(Color.WHITE);
        graphics2D.setColor(Color.WHITE);//g.setColor(Color.WHITE);
        if (startTime == 0) {
            startTime = System.currentTimeMillis();
        }
        currTime = System.currentTimeMillis();
        int maxwidth = getWidth(), rowWidth = 0, y = 0, rowHeight = 0;
        int count = sectionList.size(), start = 0, x = 0;
        for (int i = 0; i < count; i++) {
            obj = sectionList.get(i);
            if (obj instanceof String) {
                str = (String) obj;
                fm = graphics2D.getFontMetrics();
                rowHeight = Math.max(rowHeight, fm.getHeight());
                int dx = fm.stringWidth(str);
                if (rowWidth + dx <= maxwidth) {
                    rowWidth += dx;
                } else {
                    point = paintRichText(graphics2D, x, y, maxwidth, rowHeight, start, i + 1);
                    start = i + 1;
                    rowWidth = point.x;
                    rowHeight = fm.getHeight();
                    x = point.x;
                    y = point.y;
                }
            } else if (obj instanceof Animation) {
                anim = (Animation) obj;
                if (anim.getWidth() + rowWidth > maxwidth) {
                    paintRichText(graphics2D, x, y, maxwidth, rowHeight, start, i);
                    start = i;
                    x = 0;
                    y += rowHeight;
                    rowWidth = anim.getWidth();
                    rowHeight = anim.getHeight();
                } else {
                    rowHeight = Math.max(rowHeight, anim.getHeight());
                    rowWidth += anim.getWidth();
                }
            } else if (obj instanceof Integer) {// line swap
                paintRichText(graphics2D, x, y, maxwidth, rowHeight, start, i + 1);
                start = i;
                x = 0;
                y += rowHeight;
                rowWidth = 0;
                rowHeight = 0;
            } else if (obj instanceof InputBean) {
                bean = (InputBean) obj;
                if (bean.getType() == -1) {
                    continue;
                }
                str = bean.getName();
                fm = graphics2D.getFontMetrics();
                rowHeight = Math.max(rowHeight, fm.getHeight());
                int dx = fm.stringWidth(str);
                String goodNumType = StringUtils.isNotBlank(bean.getGoodNumType())?bean.getGoodNumType():"0";
                if(StringUtils.isNotBlank(bean.getGoodNum())){
                    dx = bean.getGoodNum().length() * 12;
                }
                if (rowWidth + dx <= maxwidth) {
                    rowWidth += dx;
                } else {
                    point = paintRichText(graphics2D, x, y, maxwidth, rowHeight, start, i + 1);
                    start = i + 1;
                    rowWidth = point.x;
                    rowHeight = fm.getHeight();
                    x = point.x;
                    y = point.y;
                }
                List<Animation> ids = bean.getIds();
                if (ids != null) {
                    rowWidth += 2;
                    for (int j = 0; j < ids.size(); j++) {
                        Animation animation = ids.get(j);
                        if (animation.getWidth() + rowWidth > maxwidth) {
                            paintRichText(graphics2D, x, y, maxwidth, rowHeight, start, i);
                            start = i;
                            x = 0;
                            y += rowHeight;
                            rowWidth = animation.getWidth();
                            rowHeight = animation.getHeight();
                        } else {
                            rowHeight = Math.max(rowHeight, animation.getHeight());
                            rowWidth += animation.getWidth();
                        }
                    }
                }
            }
        }
        paintRichText(graphics2D, x, y, maxwidth, rowHeight, start, count);
    }

    private String lastStr;
    private InputBean lastBean;
    String s;
    String str;
    FontMetrics fm;
    InputBean bean;
    Animation anim;
    Object obj;
    Object nextObj;
    Color color;
    int len;
    int begin = 0, index, dx = 0;



    private Point paintRichText(Graphics g, int x, int y, int width, int rowh, int start, int end) {
        if (lastStr != null) {
            if (lastBean != null) {
                color = g.getColor();
                fm = g.getFontMetrics();
                if (lastBean.getColor2() != null) {
                    g.setColor(lastBean.getColor2());
                }
                g.drawString(lastStr, 0, y + rowh);
                if (lastBean.getType() != 1) {
                    g.drawLine((lastBean.isM() ? 1 : 0), y + 3 + rowh + (lastBean.isM() ? 1 : 0), fm.stringWidth(lastStr) + (lastBean.isM() ? 1 : 0), y + 3 + rowh + (lastBean.isM() ? 1 : 0));
                }
                g.setColor(color);
            } else {
                g.drawString(lastStr, 0, y + rowh);
            }
            lastStr = null;
            lastBean = null;
        }
        for (int i = start; i < end; i++) {
            obj = sectionList.get(i);
            if (obj instanceof String) {
                str = (String) obj;
                fm = g.getFontMetrics();
                len = str.length();
                begin = 0;
                dx = 0;
                for (index = 0; index < len; ) {
                    dx = fm.charWidth(str.charAt(index));
                    while (x + dx <= width) {
                        if (++index >= len) {
                            break;
                        }
                        dx += fm.charWidth(str.charAt(index));
                    }
                    s = str.substring(begin, index);
                    if (i == end - 1 && index >= len && x + dx < width && sectionList.size() > end) {
                        lastStr = s;
                        nextObj = sectionList.get(end);
                        if (nextObj instanceof Animation) {
                            anim = (Animation) nextObj;
                            if (anim.getWidth() + x + dx > width) {
                                g.drawString(s, x, y + rowh);
                                lastStr = null;
                            }
                        }
                    } else {
                        g.drawString(s, x, y + rowh);
                    }
                    if (index < len || x + dx == width) {
                        begin = index;
                        x = 0;
                        y += rowh;
                        rowh = fm.getHeight();
                    } else {
                        x += fm.stringWidth(s);
                    }
                }
            } else if (obj instanceof Color) {
                g.setColor((Color) obj);
            } else if (obj instanceof Animation) {
                anim = (Animation) obj;
                anim.updateToTime(currTime - startTime);
                g.drawImage(anim.getImage(), x, y + 5 + rowh - anim.getHeight(), null);
                x += anim.getWidth();
            } else if (obj instanceof InputBean) {
                bean = (InputBean) obj;
                if (bean.getType() == -1) {
                    continue;
                }
                if (StringUtils.isNotBlank(bean.getGoodNum())) {
                    String goodNumType = StringUtils.isNotBlank(bean.getGoodNumType()) ? bean.getGoodNumType() : "0";
                    String str = bean.getGoodNum();
                    int wh = 12;
                    if (bean.getHeight() == null) {
                        bean.setS_x(x);
                        bean.setS_y(y - wh + rowh);
                    }
                    int len = str.length();
                    int begin = 0, index, dx = 0;
                    for (index = 0; index < len; ) {
                        dx = 12;
                        while (x + dx <= width) {
                            if (++index >= len) {
                                break;
                            }
                            dx += 12;
                        }
                        String s = str.substring(begin, index);
                        if (i == end - 1 && index >= len && x + dx < width && sectionList.size() > end) {
                            lastStr = s;
                            lastBean = bean;
                            Object nextObj = sectionList.get(end);
                            if (nextObj instanceof Animation) {
                                Animation anim = (Animation) nextObj;
                                if (anim.getWidth() + x + dx > width) {
                                    ImageIcon icon = CutButtonImage.getImage("inkImg/number/" + goodNumType + s + ".png", 11, 15);
                                    g.drawImage(icon.getImage(), x + (bean.isM() ? 1 : 0), y + (bean.isM() ? 1 : 0), null);
                                    if (bean.getType() != 1) {
                                        g.drawLine(x + (bean.isM() ? 1 : 0), y + 3 + rowh + (bean.isM() ? 1 : 0), x + 12 + (bean.isM() ? 1 : 0), y + 3 + rowh + (bean.isM() ? 1 : 0));
                                    }
                                    lastStr = null;
                                    lastBean = null;
                                }
                            }
                        } else {
                            if (s.length() > 0) {
                                String[] singleChar = s.split("");
                                for (int k = 0; k < singleChar.length; k++) {
                                    ImageIcon icon = CutButtonImage.getImage("inkImg/number/" + goodNumType + singleChar[k] + ".png", 11, 15);
                                    g.drawImage(icon.getImage(), x + k * 12 + (bean.isM() ? 1 : 0), y + (rowh - 13) + (bean.isM() ? 1 : 0), null);
                                }
                            }
                        }
                        if (index < len || x + dx == width) {
                            begin = index;
                            x = 0;
                            y += rowh;
                            rowh = 12;
                        } else {
                            x += 12 * s.length();
                        }
                    }
                    if (bean.getHeight() == null) {
                        bean.setHeight(12);
                        bean.setE_x(x);
                        bean.setE_y(y - 12 + rowh);
                    }
                } else {
                    color = g.getColor();
                    if (bean.getColor2() != null) {
                        g.setColor(bean.getColor2());
                    }
                    str = bean.getName();
                    List<Animation> ids = bean.getIds();
                    if (ids != null) {
                        str = str.substring(0, str.length() - 1);
                    }
                    fm = g.getFontMetrics();
                    if (bean.getHeight() == null) {
                        bean.setS_x(x);
                        bean.setS_y(y - fm.getHeight() + rowh);
                    }
                    len = str.length();
                    begin = 0;
                    dx = 0;
                    for (index = 0; index < len; ) {
                        dx = fm.charWidth(str.charAt(index));
                        while (x + dx <= width) {
                            if (++index >= len) {
                                break;
                            }
                            dx += fm.charWidth(str.charAt(index));
                        }
                        s = str.substring(begin, index);
                        if (i == end - 1 && index >= len && x + dx < width && sectionList.size() > end) {
                            lastStr = s;
                            lastBean = bean;
                            nextObj = sectionList.get(end);
                            if (nextObj instanceof Animation) {
                                anim = (Animation) nextObj;
                                if (anim.getWidth() + x + dx > width) {
                                    g.drawString(s, x + (bean.isM() ? 1 : 0), y + rowh + (bean.isM() ? 1 : 0));
                                    if (bean.getType() != 1) {
                                        g.drawLine(x + (bean.isM() ? 1 : 0), y + 3 + rowh + (bean.isM() ? 1 : 0), x + fm.stringWidth(s) + (bean.isM() ? 1 : 0), y + 3 + rowh + (bean.isM() ? 1 : 0));
                                    }
                                    lastStr = null;
                                    lastBean = null;
                                }
                            }
                        } else {
                            g.drawString(s, x + (bean.isM() ? 1 : 0), y + rowh + (bean.isM() ? 1 : 0));
                            if (bean.getType() != 1) {
                                g.drawLine(x + (bean.isM() ? 1 : 0), y + 3 + rowh + (bean.isM() ? 1 : 0), x + fm.stringWidth(s) + (bean.isM() ? 1 : 0), y + 3 + rowh + (bean.isM() ? 1 : 0));
                            }
                        }
                        if (index < len || x + dx == width) {
                            begin = index;
                            x = 0;
                            y += rowh;
                            rowh = fm.getHeight();
                        } else {
                            x += fm.stringWidth(s);
                        }
                    }

                    if (ids != null) {
                        x += 2;
                        for (int j = 0; j < ids.size(); j++) {
                            Animation animation = ids.get(j);
                            animation.updateToTime(currTime - startTime);
                            g.drawImage(animation.getImage(), x, y + 3 + rowh - animation.getHeight(), null);
                            if (x + animation.getWidth() >= width) {
                                x = 0;
                                y += rowh;
                                rowh = fm.getHeight();
                            } else {
                                x += animation.getWidth();
                            }
                        }
                        g.drawString("]", x + (bean.isM() ? 1 : 0), y + rowh + (bean.isM() ? 1 : 0));
                        x += fm.stringWidth("]");
                    }
                    if (bean.getHeight() == null) {
                        bean.setHeight(fm.getHeight());
                        bean.setE_x(x);
                        bean.setE_y(y - fm.getHeight() + rowh);
                    }
                    g.setColor(color);
                }
            }
        }
        point.move(x, y);
        return point;
//		return new Point(x, y);
    }

    private Point point = new Point(0, 0);

    public void setText(String text) {
        this.text = text;
        sectionList.clear();
        this.isInteractive = false;

        parseText(text);
    }

    public void setTextSize(String text, int size) {
        this.text = text;
        sectionList.clear();
        this.isInteractive = false;

        parseText(text);
        setSize(computeSize(size));
    }

    public void addText(String text) {
        this.text += text;
        parseText(text);
    }

    public void addText(final String text, final Font font) {
        this.text += text;
        this.font = font;
        this.parseText(text);
    }


    public String getText() {
        return text;
    }

    public void remove() {
        sectionList.clear();
        this.isInteractive = false;

        disable();
    }

    static BufferedImage temp = new BufferedImage(1, 1, BufferedImage.TYPE_USHORT_565_RGB);

    public static int fontWidth(Font font, String text) {
        synchronized (temp) {
            Graphics g = temp.getGraphics();
            g.setFont(font);
            FontMetrics fm = g.getFontMetrics();
            return fm.stringWidth(text);
        }
    }

    Graphics g;
    boolean is;

    public Dimension computeSize(int maxwidth) {
        if (sectionList.size() == 0)
            return new Dimension(1, 1);
        synchronized (temp) {
            int rowWidth = 0, y = 0, rowHeight = 0;
            int count = sectionList.size(), start = 0, x = 0;
            g = temp.getGraphics();
            if (font != null) {
                g.setFont(font);
            } else {
                g.setFont(UIUtils.TEXT_FONT);
            }
            is = false;
            for (int i = 0; i < count; i++) {
                Object obj = sectionList.get(i);
                if (obj instanceof String) {
                    str = (String) obj;
                    fm = g.getFontMetrics();
                    rowHeight = Math.max(rowHeight, fm.getHeight());
                    int dx = fm.stringWidth(str);
                    if (rowWidth + dx <= maxwidth) {
                        rowWidth += dx;
                    } else {
                        Point p = paintRichText(g, x, y, maxwidth, rowHeight,
                                start, i + 1);
                        start = i + 1;
                        rowWidth = p.x;
                        rowHeight = fm.getHeight();
                        x = p.x;
                        y = p.y;
                    }
                } else if (obj instanceof Animation) {
                    anim = (Animation) obj;
                    if (anim.getWidth() + rowWidth > maxwidth) {
                        paintRichText(g, x, y, maxwidth, rowHeight, start, i);
                        start = i;
                        x = 0;
                        y += rowHeight;
                        rowWidth = anim.getWidth();
                        rowHeight = anim.getHeight();
                    } else {
                        rowHeight = Math.max(rowHeight, anim.getHeight());
                        rowWidth += anim.getWidth();
                    }
                } else if (obj instanceof Integer) {// line swap
                    paintRichText(g, x, y, maxwidth, rowHeight, start, i + 1);
                    start = i;
                    x = 0;
                    y += rowHeight;
                    rowWidth = 0;
                    rowHeight = 0;
                } else if (obj instanceof InputBean) {
                    bean = (InputBean) obj;
                    if (bean.getType() == -1) {
                        is = true;
                        continue;
                    }
                    if (StringUtils.isNotBlank(bean.getGoodNum())) {
                        String str = bean.getGoodNum();
                        rowHeight = Math.max(rowHeight, 12);//
                        int dx = str.length() * 12;
                        if (rowWidth + dx <= maxwidth) {
                            rowWidth += dx;
                        } else {
                            Point p = paintRichText(g, x, y, maxwidth, rowHeight, start, i + 1);
                            start = i + 1;
                            rowWidth = p.x;
                            rowHeight = 12;
                            x = p.x;
                            y = p.y;
                        }
                    } else {
                        str = bean.getName();
                        fm = g.getFontMetrics();
                        rowHeight = Math.max(rowHeight, fm.getHeight());
                        int dx = fm.stringWidth(str);
                        if (rowWidth + dx <= maxwidth) {
                            rowWidth += dx;
                        } else {
                            Point p = paintRichText(g, x, y, maxwidth, rowHeight, start, i + 1);
                            start = i + 1;
                            rowWidth = p.x;
                            rowHeight = fm.getHeight();
                            x = p.x;
                            y = p.y;
                        }

                        List<Animation> ids = bean.getIds();
                        if (ids != null) {
                            rowWidth += 2;
                            for (int j = 0; j < ids.size(); j++) {
                                Animation animation = ids.get(j);
                                if (animation.getWidth() + rowWidth > maxwidth) {
                                    paintRichText(g, x, y, maxwidth, rowHeight, start, i);
                                    start = i;
                                    x = 0;
                                    y += rowHeight;
                                    rowWidth = animation.getWidth();
                                    rowHeight = animation.getHeight();
                                } else {
                                    rowHeight = Math.max(rowHeight, animation.getHeight());
                                    rowWidth += animation.getWidth();
                                }
                            }
                        }
                    }
                }
            }
            point = paintRichText(g, x, y, maxwidth, rowHeight, start, count);
            if (is) {
                maxwidth = 0;
            } else if (y == 0 && point.x != 0) {
                maxwidth = point.x;
            }
            return new Dimension(maxwidth, y + rowHeight + 4);// FIXME height!!1
        }
    }

    @Override
    public void paintImmediately(int x, int y, int w, int h) {
        // super.paintImmediately(x, y, w, h);
    }

    /**
     * 判断是否点击到展示
     */
    public InputBean isMonitor(int x, int y) {
        if (isInteractive) {
            int count = sectionList.size();
            for (int i = 0; i < count; i++) {
                Object obj = sectionList.get(i);
                if (obj instanceof InputBean) {
                    InputBean bean = (InputBean) obj;
                    if (bean.getType() == -1) {
                        continue;
                    }
                    if (bean.isMonitor(x, y)) {
                        return bean;
                    }
                }
            }
        }
        return null;
    }


    public String gettzm(String value) {
        String name = "";
        String namet = "";
        Pattern regex = Pattern.compile("\\【([^】]*)\\】");
        Matcher matcher = regex.matcher(value);
        while (matcher.find()) {
            if (matcher.group(1).contains("套装名称")) {
                name = matcher.group(1).split(":")[1];
                break;
            }
        }
        AllSuit allSuit = UserMessUntil.getAllSuit();
        Map<Integer, RoleSuitBean> sas = allSuit.getRolesuit();
        for (int i = 1; i < 105; i++) {
            RoleSuitBean bean = sas.get(i);
            if (bean.getSname().equals(name)) {
                namet = bean.getSuitID() + "=" + bean.getHaveParts();
                break;
            }
        }
        return namet;
    }

    public boolean getjntp(int val) {
        if (val >= 6001 && val <= 6042) {
            return true;
        } else {
            return false;
        }
    }

    public boolean getbstp(int val) {
        if (val >= 746 && val <= 769) {
            return true;
        } else {
            return false;
        }
    }
}
